package com.javaclimb.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaclimb.drug.common.ResultMapUtil;
import com.javaclimb.drug.entity.Discard;
import com.javaclimb.drug.entity.Saleinfo;
import com.javaclimb.drug.service.IDiscardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 销毁记录相关的controller
 */
@Controller
@RequestMapping(value = "/discard")
public class DiscardController {

    @Autowired
    private IDiscardService discardService;

    /**
     * 转向销毁记录页面
     */
    @RequestMapping
    public String discard(){
        return "/discard";
    }

    /**
     * 分页查询销毁记录列表
     */
    @RequestMapping(value = "/discardQueryPage")
    @ResponseBody
    public Object discardQueryPage(String param, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        try {
            IPage<Discard> iPage = discardService.selectDiscardPage(pageNum, pageSize, param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    /**
     * 转向销毁记录新增页面
     */
    @RequestMapping(value =  "/discardPage")
    public String discardPage(){
        return "/discardPage";
    }

    /**
     * 添加一个销售记录
     */
    @RequestMapping(value = "/discardAdd")
    @ResponseBody
    public Object discardAdd(Discard discard){
        try{
            int i = discardService.addDiscard(discard);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向销售记录编辑页面
     */
    @RequestMapping(value = "/discardQueryById")
    public String discardQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        Discard discard = discardService.querydiscardById(id);
        model.addAttribute("obj",discard);
        return "/discardPage";
    }

    /**
     * 修改一个销售记录
     */
    @RequestMapping(value = "/discardEdit")
    @ResponseBody
    public Object discardEdit(Discard discard){
        try{
            int i = discardService.editDiscard(discard);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个销售记录
     */
    @RequestMapping(value = "/discardDelById")
    @ResponseBody
    public Object discardDelById(Integer id){
        try{
            int i = discardService.delDiscardById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

}





















